#include "StdAfx.h"
#include "CommonDLL.h"
#include "ConcretePlugin.h"

ConcretePlugin::ConcretePlugin(void)
{
}

ConcretePlugin::~ConcretePlugin(void)
{
}

bool ConcretePlugin::SanityCheck(void)
{
	//std::cout << __FUNCTION__ << std::endl;
	return true;
}

bool ConcretePlugin::Initialize(void)
{
	//std::cout << __FUNCTION__ << std::endl;

	broadphase = new btDbvtBroadphase();
	collisionConfiguration = new btDefaultCollisionConfiguration();
	dispatcher = new btCollisionDispatcher(collisionConfiguration);
	solver = new btSequentialImpulseConstraintSolver;
	dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,broadphase,solver,collisionConfiguration);
	dynamicsWorld->setGravity(btVector3(0,-10,0));
	groundShape = new btStaticPlaneShape(btVector3(0,1,0),1);
	fallShape = new btSphereShape(1);

    btDefaultMotionState* groundMotionState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3(0,-1,0)));
    btRigidBody::btRigidBodyConstructionInfo groundRigidBodyCI(0,groundMotionState,groundShape,btVector3(0,0,0));
    groundRigidBody = new btRigidBody(groundRigidBodyCI);
    dynamicsWorld->addRigidBody(groundRigidBody);
    btDefaultMotionState* fallMotionState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3(0,50,0)));
    btScalar mass = 1;
    btVector3 fallInertia(0,0,0);
    fallShape->calculateLocalInertia(mass,fallInertia);
    btRigidBody::btRigidBodyConstructionInfo fallRigidBodyCI(mass,fallMotionState,fallShape,fallInertia);
    fallRigidBody = new btRigidBody(fallRigidBodyCI);
    dynamicsWorld->addRigidBody(fallRigidBody);

	return true;
}

bool ConcretePlugin::Update(void)
{
	//std::cout << __FUNCTION__ << std::endl;
	dynamicsWorld->stepSimulation(1/60.f,10);

	return true;
}

bool ConcretePlugin::Destroy(void)
{
	std::cout << __FUNCTION__ << std::endl;

	dynamicsWorld->removeRigidBody(fallRigidBody);
	delete fallRigidBody->getMotionState();
	delete fallRigidBody;
	delete fallShape;
	dynamicsWorld->removeRigidBody(groundRigidBody);
	delete groundRigidBody->getMotionState();
	delete groundRigidBody;
	delete groundShape;
	delete dynamicsWorld;
	delete solver;
	delete collisionConfiguration;
	delete dispatcher;
	delete broadphase;

	return true;
}
